package com.flyxteam.supports.hierarchy.abstracts;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import com.flyxteam.commons.base.consts.GlobalConsts;

/**
 * 单元测试的抽象支持类
 * <ul>
 * <li>@require TestNG</li>
 * </ul>
 * <ul>
 * <li>2017-01-01 | ChrisX | + create</li>
 * </ul>
 * @author ChrisX
 */
@ContextConfiguration({ "classpath*:spring.xml", "classpath*:springmvc.xml" })
@WebAppConfiguration("src/test/java")
@Transactional
@Rollback
public abstract class AbstractUnitTestSupport extends AbstractTestNGSpringContextTests {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    public Logger getLogger() {
        return logger;
    }
    
    /**
     * MockMvc
     */
    protected MockMvc mockMvc;
    public MockMvc getMockMvc() {
        return mockMvc;
    }
    public void setMockMvc(MockMvc mockMvc) {
        this.mockMvc = mockMvc;
    }
    
    /**
     * MvcResult
     */
    protected MvcResult mvcResult;
    public MvcResult getMvcResult() {
        return mvcResult;
    }
    public void setMvcResult(MvcResult mvcResult) {
        this.mvcResult = mvcResult;
    }
    
    /**
     * ResultActions
     */
    protected ResultActions resultActions;
    public ResultActions getResultActions() {
        return resultActions;
    }
    public void setResultActions(ResultActions resultActions) {
        this.resultActions = resultActions;
    }
    
    /**
     * MockHttpServletRequest
     */
    protected MockHttpServletRequest mockRequest;
    public MockHttpServletRequest getMockRequest() {
        return mockRequest;
    }
    public void setMockRequest(MockHttpServletRequest mockRequest) {
        this.mockRequest = mockRequest;
    }
    
    /**
     * MockHttpServletResponse
     */
    protected MockHttpServletResponse mockResponse;
    public MockHttpServletResponse getMockResponse() {
        return mockResponse;
    }
    public void setMockResponse(MockHttpServletResponse mockResponse) {
        this.mockResponse = mockResponse;
    }
    
    /**
     * MockHttpSession
     */
    protected MockHttpSession mockSession;
    public MockHttpSession getMockSession() {
        return mockSession;
    }
    public void setMockSession(MockHttpSession mockSession) {
        this.mockSession = mockSession;
    }
    
    /**
     * MockServletContext
     */
    protected MockServletContext mockServletContext;
    public MockServletContext getMockServletContext() {
        return mockServletContext;
    }
    public void setMockServletContext(MockServletContext mockServletContext) {
        this.mockServletContext = mockServletContext;
    }
    
    /**
     * WebApplicationContext
     */
    @Resource
    protected WebApplicationContext webApplicationContext;
    public WebApplicationContext getWebApplicationContext() {
        return webApplicationContext;
    }
    
    /**
     * spring加载后初始化mock实例
     */
    @PostConstruct
    private void initMockContext() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
        mockRequest = new MockHttpServletRequest();
        mockRequest.setCharacterEncoding(GlobalConsts.ENCODING);
        mockResponse = new MockHttpServletResponse();
        mockResponse.setCharacterEncoding(GlobalConsts.ENCODING);
    }

}
